package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.ProductType;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 供应商
 * 
 */
@Component
public class ProductTypeDao implements BaseDao<ProductType> {
    @Override
    public int insert(ProductType entity) {
        return DBHelper.executeUpdate("insert into product_type (name) values (?);", entity.getName());
    }

    @Override
    public int update(ProductType entity) {
        return DBHelper.executeUpdate("update product_type set name=? where id=?", entity.getName(), entity.getId());
    }

    @Override
    public int delete(ProductType entity) {
        return DBHelper.executeUpdate("update product_type set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from product_type where is_deleted=0");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {

        String sql = "select * from product_type where is_deleted=0 ";
        ProductType product_type = (ProductType)obj[0];
        if (obj[0] != null && !"".equals(product_type.getName())) {
            sql += " and name like '%" + product_type.getName() + "%'";
        }
        return DBHelper.executeQuery(sql + " limit ?,?", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(ProductType entity) {

        return DBHelper.executeQuery("select * from product_type where is_deleted=0 and id=? ", entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        return Integer.valueOf(DBHelper.executeQuery("select count(*) counts from product_type where is_deleted=0")
            .get(0).get("counts").toString());
    }
}
